package indi.jp.project.core.controller;

import indi.jp.project.core.entity.JsonResult;
import indi.jp.project.core.entity.Menu;
import indi.jp.project.core.entity.User;
import indi.jp.project.core.service.UserService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sys/user")
public class UserController {

    private UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @GetMapping("queryGrid")
    @ResponseBody
    public JsonResult queryGrid(@ModelAttribute User entity) {
        try {
            int offset = (entity.getPage() - 1) * entity.getPageSize();
            entity.setOffset(offset);
            Map<String, Object> map = userService.queryGrid(entity);
            return JsonResult.success(map);
        } catch (SQLException e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @GetMapping("/queryUserMenu")
    @ResponseBody
    public JsonResult queryUserMenu(HttpServletRequest request, HttpServletResponse response) {
        String username = request.getParameter("account");
        try {
            List<Menu> list = userService.queryUserMenu(username);
            return JsonResult.success(list);
        } catch (SQLException e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @PostMapping("/lockUser")
    @ResponseBody
    public JsonResult lockUser(HttpServletRequest request) {
        String id = request.getParameter("id");
        try {
            int effectRow = userService.lockUser(id);
            if (effectRow > 0) {
                return JsonResult.success("");
            } else {
                return JsonResult.failure("未能锁定用户，请刷新查看数据是否存在");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @PostMapping("/unlockUser")
    @ResponseBody
    public JsonResult unlockUser(HttpServletRequest request) {
        String id = request.getParameter("id");
        try {
            int effectRow = userService.unlockUser(id);
            if (effectRow > 0) {
                return JsonResult.success("");
            } else {
                return JsonResult.failure("未能解锁用户，请刷新查看数据是否存在");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @GetMapping("/queryUserByRoles")
    public JsonResult queryUserByRoles(HttpServletRequest request) {
        String roleStr = request.getParameter("roles");
        String pageStr = request.getParameter("page");
        String pageSizeStr = request.getParameter("pageSize");

        if ("".equals(roleStr)) {
            return JsonResult.failure("缺少角色编码");
        }
        if (pageStr == null || "".equals(pageStr) || pageSizeStr == null || "".equals(pageSizeStr)) {
            return JsonResult.failure("缺少分页参数");
        }
        int page = Integer.parseInt(pageStr);
        int pageSize = Integer.parseInt(pageSizeStr);
        int offset = (page - 1) * pageSize;
        Map<String, Object> params = new HashMap<>();
        String[] roles = roleStr.split(",");
        params.put("roles", roles);
        params.put("offset", offset);
        params.put("pageSize", pageSize);
        try {
            Map<String, Object> map = userService.queryUserByRoles(params);
            return JsonResult.success(map);
        } catch (SQLException e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @PostMapping("/passwordModify")
    @ResponseBody
    public JsonResult passwordMofidy(@RequestBody Map<String, String> map, HttpServletRequest request) {
        String pwdBefore = map.get("pwdBefore");
        String pwdNew = map.get("pwdNew");
        String pwdNewAgain = map.get("pwdNewAgain");
        if ("".equals(pwdBefore) || "".equals(pwdNew) || "".equals(pwdNewAgain)) {
            return JsonResult.failure("原密码和新密码不能为空");
        }
        if (pwdBefore.equals(pwdNew)) {
            return JsonResult.failure("原密码和新密码不能相同");
        }
        if (!pwdNew.equals(pwdNewAgain)) {
            return JsonResult.failure("两次输入的新密码不一致");
        }
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String password = encoder.encode(pwdNew);
        String username = request.getParameter("account");
        map.put("password", password);
        map.put("username", username);
        try {
            return userService.pwdModify(map);
        } catch (SQLException e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @PostMapping("/passwordInit")
    @ResponseBody
    public JsonResult passwordInit(@RequestBody User entity, HttpServletRequest request){
        try {
            int effectRow = userService.pwdInit(entity);
            if (effectRow > 0) {
                return JsonResult.success("");
            } else {
                return JsonResult.failure("操作失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }
}
